/**
 * Created by gongxun on 17/7/20.
 */

var PdkPlayerModel = function(){
    var that = {};

    that.seat = null;

    //手牌
    that.inhandCards = [];

    //打出去的牌
    that.dealCards = [];

    //废牌堆
    that.hideCards = [];

    //提示文字
    that.showImg = null;

    //playcard
    that.playCardNode = null;

    that.reset = function(){
        that.inhandCards = [];
        that.dealCards = [];
        that.hideCards = [];
        that.showImg.setVisible(false);
    };

    that.setPlayCardNode = function(node){
        that.playCardNode = node;

        that.showImg = new cc.Sprite("res/ccs/pdk/bg_pass.png");
        if (that.seat == 1){
            that.showImg.setAnchorPoint(cc.p(1, 0.5));
        }else if (that.seat == 2){
            that.showImg.setAnchorPoint(cc.p(0, 0.5));
        }
        that.showImg.setVisible(false);
        node.addChild(that.showImg);
    };

    that.setFapai = function(cards){
        that.inhandCards = cards;

        that.updateInhand();

        for (var i = 0; i < that.inhandCards.length; i++) {
            var card = that.inhandCards[i];
            card.setOpacity(0);
            card.runAction(cc.sequence(
                cc.delayTime(0.02 * i),
                cc.callFunc(function(){
                    this.setOpacity(255);
                }, card)
            ));
        }
    };

    that.setInhandCard = function(cards){
        that.inhandCards = cards;

        that.updateInhand();
    };

    //手牌重新排序
    that.updateInhand = function(){
        for (var i = 0; i < that.inhandCards.length; i++){
            var card = that.inhandCards[i];
            card.setCardKind(that.seat, 0);
            card.setPosition(mo.pdkCardHelper.getInhandCardPos(that.seat, i, that.inhandCards.length));
            card.setIsUpStats(false);
            card.setIsColorStats(false);
        }

        if (that.seat == 0){
            //检测出牌选项
            mo.pdkTableStatus.checkSelectIsPlay();
        }
    };

    that.setChupai = function(cardNums, isLastDealSeat){
        var isAction = (isLastDealSeat != undefined && isLastDealSeat == true);

        if (cardNums == null){
            //什么都不显示
            that.showImg.setVisible(false);
            for (var i = 0; i < that.dealCards.length; i++){
                that.dealCards[i].setVisible(false);
            }
        }else if (cardNums.length == 0){
            //显示要不起
            that.showImg.setVisible(true);
            for (var i = 0; i < that.dealCards.length; i++){
                that.dealCards[i].setVisible(false);
            }

            that.showImg.setTexture("res/ccs/pdk/bg_pass.png");
            that.showImg.setPosition(mo.pdkCardHelper.clockPos[that.seat]);
        }else{
            mo.pdkCardHelper.sortCard(cardNums);
            //显示出的牌
            that.showImg.setVisible(false);
            for (var i = 0; i < that.dealCards.length; i++){
                if (i < cardNums.length){
                    that.dealCards[i].setVisible(true);
                }else{
                    that.dealCards[i].setVisible(false);
                }
            }

            for (var i = 0; i < cardNums.length; i++){
                that.dealCards[i].setCardNum(cardNums[i]);
                that.dealCards[i].setCardKind(that.seat, 1);
                that.dealCards[i].setPosition(mo.pdkCardHelper.getDealCardPos(that.seat, i, cardNums.length));
            }
        }
    };

    return that;
};